package com.seeyon.apps.thirdapp.util;

import com.seeyon.apps.cip.messageandpending.vo.MessageReceiveVo;
import com.seeyon.apps.cip.vo.ThirdPendingVO;
import com.seeyon.apps.thirdapp.domain.MessageJSON;
import com.seeyon.apps.thirdapp.domain.MsgData;
import com.seeyon.apps.thirdapp.domain.Oacard;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.ParamUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;
import java.util.Map;


public class LanxinSendUtil {
    private static final Log log = LogFactory.getLog(LanxinSendUtil.class);
    private static final OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");

    public void pendingSend(Map<String, Object> map) {

        try {
            List<Map<String, Object>> data = (List<Map<String, Object>>) map.get("pendingList");
            List<ThirdPendingVO> voList = ParamUtil.mapsToBeans(data, ThirdPendingVO.class, false);
            long startTime = System.currentTimeMillis();
            for (ThirdPendingVO pendingVO : voList) {
                MessageJSON messageObj = null;
                Oacard oacard = null;
                MsgData msgData = null;
                String app_token = null;
                //app_token
                app_token = LanxinUtil.getLxAccessToken("MSGTOKEN", true, true).getToken();
                log.info("蓝信【oa_card_msg】app_token：" + app_token);
                messageObj = new MessageJSON();
                oacard = new Oacard();
                msgData = new MsgData();
                msgData.setOacard(oacard);
                oacard.setTitle(pendingVO.getTitle());
                oacard.setLink(pendingVO.getH5url());
                oacard.setPcLink(pendingVO.getUrl());
                oacard.setStaffId(pendingVO.getThirdReceiverId());
                messageObj.setMsgType("oacard");
                messageObj.setMsgData(msgData);
                LanxinUtil.sendLxMessage(app_token, messageObj);
            }
            long endTime = System.currentTimeMillis();
            log.info("蓝信【oa_card_msg】 执行共计花费时间： " + (endTime - startTime) + " ms");
        } catch (Exception e) {
            log.info("蓝信消息发送出现异常，异常信息：" + e);
        }
    }

    public void messageSend(Map<String, Object> map) {
        try {
            MessageReceiveVo messageReceiveVo = new MessageReceiveVo();
            ParamUtil.mapToBean(map, messageReceiveVo, false);
            long startTime = System.currentTimeMillis();
            MessageJSON messageObj = null;
            Oacard oacard = null;
            MsgData msgData = null;
            String app_token = null;
            //app_token
            app_token = LanxinUtil.getLxAccessToken("MSGTOKEN", true, true).getToken();
            log.info("蓝信【oa_card_msg】app_token：" + app_token);
            messageObj = new MessageJSON();
            oacard = new Oacard();
            msgData = new MsgData();
            msgData.setOacard(oacard);
            oacard.setTitle(messageReceiveVo.getMessageContent());
            oacard.setLink(messageReceiveVo.getMessageH5URL());
            oacard.setPcLink(messageReceiveVo.getMessageURL());
            oacard.setStaffId(messageReceiveVo.getThirdpartyReceiverId());
            messageObj.setMsgType("oacard");
            messageObj.setMsgData(msgData);
            LanxinUtil.sendLxMessage(app_token, messageObj);
        } catch (Exception e) {
            log.info("蓝信消息发送出现异常，异常信息：" + e);
        }
    }
}
